﻿'*******************************************************************************************
'* Copyright Brügmann Software GmbH, Papenburg
'* Author: Björn Kremer
'* Contact: codeplex<at>patorg.de
'* 
'* VB.CMIS is a VB.NET implementation of the Content Management Interoperability Services (CMIS) standard
'*
'* This file is part of VB.CMIS.
'* 
'* VB.CMIS is free software: you can redistribute it and/or modify
'* it under the terms of the GNU Lesser General Public License as published by
'* the Free Software Foundation, either version 3 of the License, or
'* (at your option) any later version.
'* 
'* VB.CMIS is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'* GNU Lesser General Public License for more details.
'* 
'* You should have received a copy of the GNU Lesser General Public License
'* along with VB.CMIS. If not, see <http://www.gnu.org/licenses/>.
'*******************************************************************************************
Imports System.Xml.Serialization
Namespace CMISObjects

   <XmlRoot("acl", Namespace:=CmisNamespaceConstants.Cmis)>
   Public Class Acl
      Implements IXmlSerializable

      Public Property ACL As AccessControlList()

      Public Sub New()

      End Sub
      Public Sub New(ACL As AccessControlList())
         Me.ACL = ACL
      End Sub

      Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema
         Return Nothing
      End Function

      Public Sub ReadXml(reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml
         Dim doc As New Xml.XmlDocument()
         doc.Load(reader)

         Dim element As Xml.XmlNode = Common.GetItemCaseInsensitiveFromXML(doc, "acl")
         Dim acl As New List(Of AccessControlList)

         For Each child As Xml.XmlNode In element.ChildNodes
            If Common.GetNodeNameWithoutPrefix(child).ToLowerInvariant.Equals("permission") Then
               Using stream As New IO.StringReader(child.OuterXml)
                  Dim serializer As New XmlSerializer(GetType(AccessControlList))
                  acl.Add(DirectCast(serializer.Deserialize(stream), AccessControlList))
               End Using
            End If
         Next

         Me.ACL = acl.ToArray

      End Sub

      Public Sub WriteXml(writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml
         Dim serializer As New XmlSerializer(GetType(AccessControlList))
         For Each item As AccessControlList In ACL
            serializer.Serialize(writer, item)
         Next

      End Sub
   End Class
End Namespace